class Pagination{
    constructor(positionList){
        this.container=$("#pagination-container");
        this.createDOM().then($.proxy(this.init,this));
        this.positionList=positionList;
       
    }
    createDOM(){
        return new Promise(resolve=>{
            // this.container.load("/html/component/list/pagination.html",resolve);
            $.get("/html/component/list/pagination.html",resolve);
        })
    }
    init(paginationTemplate){
        let {current,total} =this.positionList.page;
        let html=new EJS({text:paginationTemplate}).render({current,total});
        this.container.html(html);
        this.prevPageBtn=$("#prev-page");//上一页按钮
        this.nextPageBtn=$("#next-page");//下一页按钮
        this.pageBtns=$("#pagination-container").find(".page");//分页按钮
        this.bindEvents();
    }
    bindEvents(){
        this.prevPageBtn.on("click",$.proxy(this.handlePreBtnClick,this));
        // this.prevPageBtn.on("click",this.handlePreBtnClick.bind(this));
        this.nextPageBtn.on("click",$.proxy(this.handleNextBtnClick,this));
    }
    handlePreBtnClick(){
        //前一页
        if(--this.positionList.page.current<1){
            //从第二页减到第一页
            //prevBtn禁用
            this.positionList.page.current=1;
            return;
        }
        //使用新的current去调用positionList里面的方法去请求新一页的数据
        this.positionList.createDOM().then($.proxy(this.positionList.init,this.positionList));
    }
    handleNextBtnClick(){
         //下一页
         if(++this.positionList.page.current>this.positionList.page.total){
            //从第二页加到最后一页
            //nextBtn禁用
            this.positoionList.page.current = this.positoionList.page.total;
            return;
        }
        //使用新的current去调用positionList里面的方法去请求新一页的数据
        this.positionList.createDOM().then($.proxy(this.positionList.init,this.positionList));
    }
}